#include "MySQLClient.hpp"
#include <mysql/field_types.h>
#include <mysql/mysql.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
using std::cout;
using std::endl;
namespace wd{


MySQLClient::MySQLClient()
{
    MYSQL * pconn = mysql_init(&_conn);
    if(!pconn){
        cout << "mysqlclient init error" << endl;
    }
}
   
MySQLClient::~MySQLClient(){
    mysql_close(&_conn);
}

    
bool MySQLClient::connect(const string & host,
                 unsigned short port,
                 const string & user,
                 const string & passwd,
                 const string & db
                 )
{
    MYSQL * pconn = mysql_real_connect(&_conn,
                               host.c_str(),
                               user.c_str(),
                               passwd.c_str(),
                               db.c_str()
                               ,0,NULL,0);
    if(!pconn){
        fprintf(stderr,"ERROR %d : %s\n",
                    mysql_errno(&_conn),
                    mysql_error(&_conn));
        return false;
    }else{
        return true;
        printf("mysql connect success!\n");
    }
}

bool MySQLClient::writeQuery(const string & sql){
    int ret = mysql_real_query(&_conn,sql.c_str(),sql.size());
    if(ret != 0){
        fprintf(stderr,"ERROR %d : %s\n",
                    mysql_errno(&_conn),
                    mysql_error(&_conn));
        return false;
    }
    printf("QUERY OK, %ld row affected,\n",
           mysql_affected_rows(&_conn));
    return true;
}


vector<vector<string>> MySQLClient::readQuery(const string & sql){
    printf("111:start read mysql\n");
    /* int ret = writeQuery(sql); */
    int ret = mysql_real_query(&_conn,sql.c_str(),sql.size());
    if(ret != 0){
        fprintf(stderr,"ERROR %d : %s\n",
                    mysql_errno(&_conn),
                    mysql_error(&_conn));
        return vector<vector<string>>(); 
    }
    printf("222: success read info from mysql\n");
    vector<vector<string>> queryRes;
    MYSQL_RES * res = mysql_use_result(&_conn);
    if(!res){
        fprintf(stderr,"ERROR %d : %s\n",
                    mysql_errno(&_conn),
                    mysql_error(&_conn));
        return vector<vector<string>>(); 

    }
    //程序运行到此处说明查到信息了
    vector<string> fields;
    int cols = mysql_num_fields(res);
    printf("cols of mysqlres:%d\n",cols);
    //先获取字段名
    MYSQL_FIELD * filed = nullptr;
    while((filed = mysql_fetch_field(res)) != nullptr){
        fields.emplace_back(filed->name);
    }
    queryRes.push_back(fields); //将一行信息放入容器中
    printf("333:get fields\n");
    //打印每行的数据
    MYSQL_ROW row;
    while((row = mysql_fetch_row(res)) != nullptr){
            vector<string> one_line;
            for(int i =0 ;i< cols; ++i){
                one_line.emplace_back(row[i]);
                /* printf("%s\t",row[i]); */
            }
            /* printf("\n"); */
            //每获取一行数据，就放入容器中
            queryRes.push_back(one_line);
    }
    printf("444: get mysql datas\n");
    mysql_free_result(res);
    return queryRes;

}
void MySQLClient::show_res(const vector<vector<string>> & res){
    cout << "result set has "<< res.size() << "rows" << endl;
    for(auto & line : res){
        for(auto & s : line){
            cout << s << "\t";
        }
        cout << endl;
    }
}

}


